// pages/ShoppingCart/ShoppingCart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Shopping: [],
    sodData: 0,
    totalPrice: 0,
    totalNum: 0,
    list: [],
    flag: true
  },
  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
    this.Shoppingcartdata()
    this.total()

  },
  onClose(event) {
    const { position, instance } = event.detail;
    switch (position) {
      case 'left':
      case 'cell':
        instance.close();
        break;
      case 'right':
        Dialog.confirm({
          message: '确定删除吗？',
        }).then(() => {
          instance.close();
        });
        break;
    }
  },
  Shoppingcartdata() {
    wx.showLoading({
      title: '加载中...',
    })
    let token = wx.getStorageSync('token') || []
    wx.request({
      url: `https://api.it120.cc/zbt/shopping-cart/info`,
      method: "get",
      data: {
        token: token
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {

        console.log('items',res);
        this.data.flag = false
        this.setData({
          Shopping: res.data.data.items
        })
        this.total()
      }
    })

    wx.hideLoading()
  },
  // 加减
  onChange(e) {
    console.log(e.detail);
    console.log(e.currentTarget.dataset.item.key);
    let token = wx.getStorageSync('token')
    wx.request({
      url: `https://api.it120.cc/zbt/shopping-cart/modifyNumber`,
      method: "POST",
      data: {
        token: token,
        key: e.currentTarget.dataset.item.key,
        number: e.detail
      },
      header: {
        token: token,
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);
        this.Shoppingcartdata()
        this.total()

      }
    })


  },
  // 合计
  total() {
    let quantity = 0
    let Price = 0
    this.data.Shopping.forEach(item => {
      // console.log(item);
      if(item.selected==true){
        quantity += item.number
        Price += item.number * item.price

      }else{
    
        Price += 0
        
      }
  
    })
    this.setData({
      totalPrice: Price,
      totalNum: quantity
    })
    wx.setTabBarBadge({
      index: 3,
      text: this.data.totalNum + '',
    })
  },

  // 单选
  onChangeAs(e) {
    // console.log(e.detail);
    let token = wx.getStorageSync('token')
    wx.request({
      url: `https://api.it120.cc/zbt/shopping-cart/select`,
      method: "POST",
      data: {
        token: token,
        key: e.currentTarget.dataset.item.key,
        selected: e.detail
      },
      header: {
        token: token,
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        // console.log(res.selected);
        this.Shoppingcartdata()
        this.total()
      }
    })







  },
  // 结账
  onClickButton() { },

  //滑动

  onCloseevent(event) {
    const { position, instance } = event.detail;
    switch (position) {
      case 'left':
      case 'cell':
        instance.close();
        break;
      case 'right':
        // Dialog.confirm({
        //   message: '确定删除吗？',
        // }).then(() => {
        instance.close();
        // });
        break;
    }
  },

  // 单删除购物车
  delete(e) {
    // console.log(e.currentTarget.dataset.item);
    let token = wx.getStorageSync('token')
    wx.request({
      url: `https://api.it120.cc/zbt/shopping-cart/remove`,
      method: "POST",
      data: {
        token: token,
        key: e.currentTarget.dataset.item.key
      },
      header: {
        token: token,
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);
        if (res.statusCode == 200) {
          wx.showLoading({
            title: '删除成功',
          })
          wx.hideLoading()
        } else {
          wx.showLoading({
            title: '删除失败',
          })
          wx.hideLoading()
        }

        this.Shoppingcartdata()
        this.total()

      }
    })

  },

  //去逛逛
  Goforawalk() {
    wx.switchTab({
      url: '/pages/homepage/homepage',
    })



  },
  // 清空购物车
  imgempty() {

    let token = wx.getStorageSync('token')
    wx.request({
      url: `https://api.it120.cc/zbt/shopping-cart/empty`,
      method: "POST",
      data: {
        token: token,
      },
      header: {
        token: token,
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);


        this.Shoppingcartdata()
        this.total()

      }
    })


  },





  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.Shoppingcartdata()
    this.total()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})